<!--
 * @Description: 本地工单维修历史详情
 * @Autor: 何兴家
 * @Date: 2020-12-18 10:57:14
 * @LastEditors: 何兴家
 * @LastEditTime: 2021-02-26 18:42:15
-->
<template>
  <div>
    <BreakSpace label="工单信息" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
    <FormPanel v-loading="allLoading" ref="orderForm" :list="orderFormList" :labelWidth="100" :isSubmitBtn="false" formType="show"></FormPanel>

    <el-tabs v-model="activeTab" v-loading="allLoading">
      <el-tab-pane label="维修项目" name="维修项目">
        <FilterTable
          ref="repairLabourTable"
          columnsRef="repair_repairReception_customer_repairHistory_orderDetail_table1"
          height="auto"
          :isSelectColumn="false"
          :columns="repairLabourColumns"
          :dataSource="labourDataSource"
          :onColumnsChange="columns => (this.repairLabourColumns = columns)"
        >
        </FilterTable>
      </el-tab-pane>
      <el-tab-pane label="维修材料" name="维修材料">
        <FilterTable
          ref="repairPartTable"
          columnsRef="repair_repairReception_customer_repairHistory_orderDetail_table2"
          height="auto"
          :isSelectColumn="false"
          :columns="repairPartColumns"
          :dataSource="partDataSource"
          :onColumnsChange="columns => (this.repairPartColumns = columns)"
        >
        </FilterTable>
      </el-tab-pane>
      <el-tab-pane label="附加项目" name="附加项目">
        <FilterTable
          ref="addItemsTable"
          columnsRef="repair_repairReception_customer_repairHistory_orderDetail_table3"
          height="auto"
          :isSelectColumn="false"
          :columns="addItemsColumns"
          :dataSource="addItemDataSource"
          :onColumnsChange="columns => (this.addItemsColumns = columns)"
        >
        </FilterTable>
      </el-tab-pane>
    </el-tabs>

    <BreakSpace label="结算单信息" :containerStyle="{ marginBottom: '10px' }"></BreakSpace>
    <FormPanel v-loading="allLoading" ref="ballanceForm" :list="balanceFormList" :labelWidth="100" :isSubmitBtn="false" formType="show"></FormPanel>

    <BaseDialog :visible.sync="showPanel" title="标题" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"> </BaseDialog>
    <div class="dialogBottmView">
      <el-button @click="handleCancel">取消</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { confirmAction, notifyAction, findFormItem, findTableItem } from '@/utils';
import { queryDiscountByRoNo } from '@/api/repair/repairReception/feesSettlement/index.js';
import { queryLocalRepairHistoryDetail } from '@/api/repair/repairReception/customer';
import { getDealerUser, getNameByIds } from '@/api/repair/common';

export default {
  props: ['orderInfo', 'repairTypeDict', 'sfqfDict'],
  mixins: [dictionary],
  data() {
    return {
      activeTab: '维修项目',
      orderFormList: this.createOrderFormList(),
      balanceFormList: this.createBalanceFormList(),

      repairLabourColumns: this.createRepairLabourColumns(),
      repairPartColumns: this.createRepairPartColumns(),
      addItemsColumns: this.createAddItemsColumns(),

      labourDataSource: [], // 维修配件数据源
      partDataSource: [], // 维修配件数据源
      addItemDataSource: [], // 附加项目数据源

      allLoading: false,
      showPanel: false // 派工详情弹窗未完成
    };
  },
  mounted() {
    this.initQuery();
    this.initDictItems();
    this.queryDealerUser();
    this.initPayDict();
  },
  methods: {
    createOrderFormList() {
      return [
        {
          type: 'INPUT',
          label: '工单号',
          fieldName: 'RO_NO'
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'LICENSE'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'VIN'
        },
        {
          type: 'INPUT',
          label: '发动机号',
          fieldName: 'ENGINE_NO'
        },
        {
          type: 'SELECT',
          label: '维修类型',
          fieldName: 'REPAIR_TYPE_CODE',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '进厂里程',
          fieldName: 'IN_MILEAGE'
        },
        {
          type: 'SELECT',
          label: '服务技师',
          fieldName: 'SERVICE_ADVISOR',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '开单时间',
          fieldName: 'RO_CREATE_DATE'
        },
        {
          type: 'INPUT',
          label: '出厂里程',
          fieldName: 'OUT_MILEAGE'
        },
        {
          type: 'INPUT',
          label: '预计交车时间',
          fieldName: 'END_TIME_SUPPOSED'
        },
        {
          type: 'INPUT',
          label: '车主',
          fieldName: 'OWNER_NAME'
        },
        {
          type: 'INPUT',
          label: '送修人姓名',
          fieldName: 'DELIVERER'
        },
        {
          type: 'INPUT',
          label: '送修人电话',
          fieldName: 'DELIVERER_PHONE'
        },
        {
          type: 'INPUT',
          label: '送修人手机',
          fieldName: 'DELIVERER_MOBILE'
        },
        {
          type: 'INPUT',
          label: '检查结果',
          fieldName: 'CHECK_RESULT'
        },
        {
          type: 'INPUT',
          label: '客户陈述',
          fieldName: 'CUSTOMER_DESC'
        }
      ];
    },
    createBalanceFormList() {
      return [
        {
          type: 'INPUT',
          label: '结算单号',
          fieldName: 'BALANCE_NO'
        },
        {
          type: 'INPUT',
          label: '结算时间',
          fieldName: 'SQUARE_DATE'
        },
        {
          type: 'SELECT',
          label: '优惠模式',
          fieldName: 'DISCOUNT_MODE_CODE',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '结算员',
          fieldName: 'BALANCE_HANDLER',
          itemList: []
        },
        // {
        //   type: 'INPUT',
        //   label: '原结算单号',
        //   fieldName: 'LAST_BALANCE_NO'
        // },
        // {
        //   type: 'INPUT',
        //   label: '发票编号',
        //   fieldName: 'INVOICE_NO'
        // },
        // {
        //   type: 'INPUT',
        //   label: '发票类型',
        //   fieldName: 'INVOICE_TYPE_CODE'
        // },
        {
          type: 'SELECT',
          label: '支付方式',
          fieldName: 'PAY_TYPE_CODE',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '应收金额',
          fieldName: 'RECEIVE_AMOUNT'
        },
        {
          type: 'INPUT',
          label: '减免金额',
          fieldName: 'DERATE_AMOUNT'
        }
      ];
    },
    createRepairLabourColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '收费区分',
          dataIndex: 'CHARGE_PARTITION_CODE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },
        {
          title: '项目维修类型',
          dataIndex: 'REPAIR_TYPE_CODE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          dictItems: []
        },
        {
          title: '项目代码',
          dataIndex: 'LABOUR_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '项目名称',
          dataIndex: 'LABOUR_NAME',
          width: 200,
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '套餐代码',
          dataIndex: 'PRODUCT_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '工时单价',
          dataIndex: 'LABOUR_PRICE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '标准工时',
          dataIndex: 'STD_LABOUR_HOUR',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '工时费',
          dataIndex: 'LABOUR_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '派工工时',
          dataIndex: 'ASSIGN_LABOUR_HOUR',
          sorter: true,
          filter: true,
          filterType: 'number'
        },

        {
          title: '优惠金额',
          dataIndex: 'DISCOUNT_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣率',
          dataIndex: 'DISCOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '实收金额',
          dataIndex: 'RECEIVE_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '责任技师',
          dataIndex: 'TECHNICIAN',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '是否派工',
          dataIndex: 'ASSIGN_TAG',
          sorter: true,
          dictItems: this.createDictList(1004)
        },
        {
          title: '是否增项',
          dataIndex: 'IS_ADDITION',
          sorter: true,
          dictItems: this.createDictList(1004)
        },
        {
          title: '是否委外',
          dataIndex: 'CONSIGN_EXTERIOR',
          sorter: true,
          dictItems: this.createDictList(1004)
        },
        {
          title: '是否不修',
          dataIndex: 'NEEDLESS_REPAIR',
          sorter: true,
          dictItems: this.createDictList(1004)
        },
        {
          title: '不修原因',
          dataIndex: 'REASON',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8070')
        },
        {
          title: '活动编号',
          dataIndex: 'ACTIVITY_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          width: 280,
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        }
      ];
    },
    createRepairPartColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '收费区分',
          dataIndex: 'CHARGE_PARTITION_CODE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },
        {
          title: '仓库代码',
          dataIndex: 'STORAGE_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '主要配件',
          dataIndex: 'IS_MAIN_PART',
          sorter: true,
          dictItems: this.createDictList(1004)
        },
        {
          title: '配件数量',
          dataIndex: 'PART_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件销售单价',
          dataIndex: 'PART_SALES_PRICE',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2
        },
        {
          title: '配件销售金额',
          dataIndex: 'PART_SALES_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2
        },
        {
          title: '折扣率',
          dataIndex: 'DISCOUNT',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '优惠金额',
          dataIndex: 'DISCOUNT_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2
        },
        {
          title: '实收金额',
          dataIndex: 'RECEIVE_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2
        },
        {
          title: '价格系数',
          dataIndex: 'PRICE_RATE',
          hidden: true,
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2
        },

        {
          title: '是否入账',
          dataIndex: 'IS_FINISHED',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1004),
          dictItems: this.createDictList(1004)
        },
        {
          title: '维修方式',
          dataIndex: 'REPAIR_PART_WAY',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8093')
        },
        {
          title: '不可打折',
          dataIndex: 'IS_DISCOUNT',
          sorter: true,
          filter: true,
          disabled: true,
          editItems: [
            { text: '', trueValue: '10041001' },
            { text: '', falseValue: '10041002' }
          ]
        },
        {
          title: '是否增项',
          dataIndex: 'IS_ADDITION',
          sorter: true,
          dictItems: this.createDictList(1004)
        },
        {
          title: '库位代码',
          dataIndex: 'STORAGE_POSITION_CODE',
          sorter: true
        },
        {
          title: '是否不修',
          dataIndex: 'NEEDLESS_REPAIR',
          sorter: true,
          dictItems: this.createDictList(1004)
        },
        {
          title: '不修类型',
          dataIndex: 'REASON_TYPE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('8070')
        },
        {
          title: '不修原因',
          dataIndex: 'REASON',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '流水号',
          dataIndex: 'BATCH_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          hidden: true,
          showOverflowTooltip: true
        },
        {
          title: '捡料单流水号',
          dataIndex: 'PART_BATCH_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '套餐代码',
          dataIndex: 'PRODUCT_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '活动编号',
          dataIndex: 'ACTIVITY_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '维修项目名称',
          dataIndex: 'LABOUR_CODE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },
        {
          title: '创建时间',
          dataIndex: 'CREATED_AT',
          sorter: true
        }
      ];
    },
    createAddItemsColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '收费区分',
          dataIndex: 'CHARGE_PARTITION_CODE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          showOverflowTooltip: true,
          dictItems: []
        },
        {
          title: '附加项目代码',
          dataIndex: 'ADD_ITEM_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '附加项目名称',
          dataIndex: 'ADD_ITEM_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '附加项目费',
          dataIndex: 'ADD_ITEM_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true,
          precision: 2
        },
        {
          title: '优惠金额',
          dataIndex: 'DISCOUNT_AMOUNT',
          filter: true,
          filterType: 'input',
          sorter: true,
          showOverflowTooltip: true,
          numberFormat: true,
          precision: 2
        },
        {
          title: '折扣率',
          dataIndex: 'DISCOUNT',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '实收金额',
          dataIndex: 'RECEIVE_AMOUNT',
          filter: true,
          filterType: 'input',
          sorter: true,
          showOverflowTooltip: true,
          numberFormat: true,
          precision: 2
        }
      ];
    },
    // 翻译优惠模式
    async initPayDict() {
      const res = await queryDiscountByRoNo('135543');
      if (res.resultCode == '200') {
        findFormItem(this.balanceFormList, 'DISCOUNT_MODE_CODE').itemList = res.data.discountModeList.map(y => ({ text: y.DISCOUNT_MODE_NAME, value: y.DISCOUNT_MODE_CODE }));
      }
    },
    // 初始化数据字典
    initDictItems() {
      findFormItem(this.orderFormList, 'REPAIR_TYPE_CODE').itemList = this.repairTypeDict;
      // 维修类型赋值
      let repairTypeTableTargetRows = [findTableItem(this.repairLabourColumns, 'REPAIR_TYPE_CODE')];
      repairTypeTableTargetRows.forEach(item => {
        item.dictItems = item.filterItems = this.repairTypeDict;
      });

      // 收费区分赋值
      let sfqfTableTargetRows = [
        findTableItem(this.repairLabourColumns, 'CHARGE_PARTITION_CODE'),
        findTableItem(this.repairPartColumns, 'CHARGE_PARTITION_CODE'),
        findTableItem(this.addItemsColumns, 'CHARGE_PARTITION_CODE')
      ];

      sfqfTableTargetRows.forEach(item => {
        item.dictItems = item.filterItems = this.sfqfDict;
      });
      //支付方式翻译
      let payList = this.createDictList(8044).map(y => ({ text: y.text, value: y.value + '' }));
      findFormItem(this.balanceFormList, 'PAY_TYPE_CODE').itemList = payList;
    },
    // 查询所有人员(翻译用)
    async queryDealerUser() {
      let res = await getDealerUser();
      if (res.resultCode == 200) {
        let userDicts = res.data.map(item => ({ text: item.employeeName, value: item.userId + '' }));
        findFormItem(this.orderFormList, 'SERVICE_ADVISOR').itemList = userDicts;
      }
    },
    // 初始化查询数据
    async initQuery() {
      this.allLoading = true;
      try {
        let res = await queryLocalRepairHistoryDetail({ roNo: this.orderInfo.RO_NO });
        if (res.resultCode == 200) {
          let targetFormList = [...this.orderFormList, ...this.balanceFormList];
          res.data.BALANCE_HANDLER && this.queryBananceHandler(res.data.BALANCE_HANDLER);

          targetFormList.forEach(item => {
            item.initialValue = res.data[item.fieldName];
          });
          this.labourDataSource = res.data.labourList;
          this.partDataSource = res.data.partList;
          this.addItemDataSource = res.data.addLabourlist;
        }
      } catch (err) {
        console.log(err, '初始化查询失败');
      } finally {
        this.allLoading = false;
      }
    },
    // 查询结算员翻译
    async queryBananceHandler(userId) {
      let res = await getNameByIds({ userIds: userId });
      if (res.resultCode == 200) {
        findFormItem(this.balanceFormList, 'BALANCE_HANDLER').itemList = res.data.map(item => ({ text: item.employeeName, value: item.employeeId + '' }));
      }
    },
    // 点击取消
    handleCancel() {
      this.$emit('close');
    }
  }
};
</script>

<style scoped></style>
